<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form
          class="mb-[-16px]"
          :model="queryParams"
          inline
      >
        <el-form-item label="广告位置说明" prop="title">
          <el-input class="w-56" v-model="queryParams.title" clearable placeholder="请输入广告位置说明"/>
        </el-form-item>

        <el-form-item label="状态：" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option label="暂停" value="0">暂停</el-option>
            <el-option label="启用" value="1">启用</el-option>
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <el-button v-perms="['adv.adv/add']" type="primary" @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus"/>
        </template>
        新增
      </el-button>
<!--      <el-button-->
<!--          v-perms="['adv.adv/delete']"-->
<!--          :disabled="!selectData.length"-->
<!--          @click="handleDelete(selectData)"-->
<!--      >-->
<!--        删除-->
<!--      </el-button>-->
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column label="" prop="id"/>
          <el-table-column label="广告位置说明" prop="title"/>
          <el-table-column label="容量" prop="num"/>
          <el-table-column label="状态" prop="status">
            <template #default="{ row }">
              <span v-if="row.status === 1">启用</span>
              <span v-else>暂停</span>
            </template>
          </el-table-column>
          <el-table-column label="像素" prop="pixel"/>
          <el-table-column label="创建时间" prop="created_at"/>
          <el-table-column label="修改时间" prop="updated_at"/>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                  v-perms="['adv.adv/edit']"
                  type="primary"
                  link
                  @click="handleEdit(row)"
              >
                编辑
              </el-button>
<!--              <el-button-->
<!--                  v-perms="['adv.adv/delete']"-->
<!--                  type="danger"-->
<!--                  link-->
<!--                  @click="handleDelete(row.id)"-->
<!--              >-->
<!--                删除-->
<!--              </el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false"/>
  </div>
</template>

<script lang="ts" setup>
import {usePaging} from '@/hooks/usePaging'
import {dictDataLists} from '@/api/setting/dict'
import {apiAdvLists, apiAdvDelete} from '@/api/adv'
import {timeFormat} from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
  title: '',
  status: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({id}) => id)
}

// 字典数据
const dictData = reactive<Record<string, any[]>>({})

// 获取字典数据
const getDictData = () => {

}

// 分页相关
const {pager, getLists, resetParams, resetPage} = usePaging({
  fetchFun: apiAdvLists,
  params: queryParams
})

// 添加
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm('确定要删除？')
  await apiAdvDelete({id})
  getLists()
}

getLists()
getDictData()
</script>

